package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.Goods;
import com.luo.model.OneClass;
import com.luo.model.Result;
import com.luo.model.TwoClass;
import com.luo.service.ClassService;
import com.luo.service.GoodService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.junit.runner.Request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 功能：类别的删除
 * 作者：Luo。
 * 日期：2024/1/17 20:10
 */
@WebServlet("/deleteClass")
public class DeleteClassController extends HttpServlet {

    ClassService classService = new ClassService();
    GoodService goodService = new GoodService();

    @Override
    protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        Result result = new Result();

        if ("one".equals(type)) {
            //删除一个
            String oneClassId = req.getParameter("oneClassId");
            String id = req.getParameter("id");
            if (oneClassId == null || oneClassId == "" || oneClassId == "undefined") {
                //删除一级分类
                //检查是否存在二级分类属于该分类
                OneClass oneClass = new OneClass();
                oneClass.setId(Integer.parseInt(id));
                List<TwoClass> twoClasses = classService.queryTwoClassByOneId(oneClass);
                //查询是否有商品属于该分类
                List<Goods> goods = goodService.queryGoodByOneId(id);
                if (twoClasses.size() > 0 || goods.size() > 0) {
                    result.setCode("-1");
                    result.setMsg("【无法删除】\n有" + twoClasses.size() + "条二级分类属于该分类\n有" + goods.size() + "条商品属于该分类");
                    String jsonString = JSON.toJSONString(result);
                    resp.getWriter().write(jsonString);
                    resp.getWriter().close();
                    return;
                }
                //如果没有则删除
                List<String> list = new ArrayList<>();
                list.add(id);
                int i = classService.deleteOneClass(list);
                result.setCode("" + i);
                result.setMsg("删除成功");
                String jsonString = JSON.toJSONString(result);
                resp.getWriter().write(jsonString);
                resp.getWriter().close();
                return;
            } else {
                //删除二级分类
                //查询是否有商品属于该分类
                List<Goods> goods = goodService.queryGoodsByTwoId(id);
                if (goods.size() > 0) {
                    result.setCode("-1");
                    result.setMsg("【无法删除】\n有" + goods.size() + "条商品属于该分类");
                    String jsonString = JSON.toJSONString(result);
                    resp.getWriter().write(jsonString);
                    resp.getWriter().close();
                    return;
                }
                //如果没有则删除
                List<String> list = new ArrayList<>();
                list.add(id);
                int i = classService.deleteTwoClass(list);
                result.setCode("" + i);
                result.setMsg("删除成功");
                String jsonString = JSON.toJSONString(result);
                resp.getWriter().write(jsonString);
                resp.getWriter().close();
            }
        } else if ("some".equals(type)) {
            //批量删除
            //先判断是一级分类还是二级分类
            String kind = req.getParameter("kind");
            String ids = req.getParameter("ids");
            String[] split = ids.split(",");
            List<String> list = Arrays.asList(split);
            if (kind == null || "".equals(kind)) {
                //判断是否有二级分类和商品属于该一级分类下
                OneClass oneClass = new OneClass();
                for (String id : list) {
                    oneClass.setId(Integer.parseInt(id));
                    List<TwoClass> twoClasses = classService.queryTwoClassByOneId(oneClass);
                    List<Goods> goods = goodService.queryGoodByOneId(id);
                    if (twoClasses.size() > 0 || goods.size() > 0) {
                        result.setCode("-1");
                        OneClass oneClass1 = classService.queryOneClassByOneId(id);
                        result.setMsg("【无法删除" + oneClass1.getName() + "】\n有" + twoClasses.size() + "条二级分类属于该分类\n有" + goods.size() + "条商品属于该分类");
                        String jsonString = JSON.toJSONString(result);
                        resp.getWriter().write(jsonString);
                        resp.getWriter().close();
                        return;
                    }
                }
                //一级分类批量删除
                int i = classService.deleteOneClass(list);
                result.setCode("" + i);
                result.setMsg("删除成功");
                String jsonString = JSON.toJSONString(result);
                resp.getWriter().write(jsonString);
                resp.getWriter().close();
            } else {
                //判断是否有商品属于该二级分类下
                for (String id : list) {
                    List<Goods> goods = goodService.queryGoodsByTwoId(id);
                    if (goods.size() > 0) {
                        result.setCode("-1");
                        TwoClass twoClass = classService.queryTwoClassByTwoId(id);
                        result.setMsg("【无法删除" + twoClass.getName() + "】\n有" + goods.size() + "条商品属于该分类");
                        String jsonString = JSON.toJSONString(result);
                        resp.getWriter().write(jsonString);
                        resp.getWriter().close();
                        return;
                    }
                }
                //二级分类批量删除
                int i = classService.deleteTwoClass(list);
                result.setCode("" + i);
                result.setMsg("删除成功");
                String jsonString = JSON.toJSONString(result);
                resp.getWriter().write(jsonString);
                resp.getWriter().close();
            }
        }
    }
}
